package com.shengdong.water.feature.main


import androidx.viewpager2.widget.ViewPager2
import com.shengdong.water.base.BaseVMActivity
import com.shengdong.water.databinding.ActivityMainBinding
import com.shengdong.water.code.viewmodel.MainViewModel
import com.shengdong.water.R
import com.shengdong.water.feature.main.fragment.MapFragment
import com.shengdong.water.feature.main.fragment.WorkbenchFragment
import dagger.hilt.android.AndroidEntryPoint
import androidx.core.view.get
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route

import com.shengdong.water.code.adapter.MainViewPagerAdapter
import com.shengdong.water.code.base.BaseActivity
import com.shengdong.water.code.config.RouterConfig
import com.shengdong.water.code.config.UserConfig
import com.shengdong.water.code.router.RouterManager
import android.util.Log

@Route(path = RouterConfig.MAIN_PATH)
@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding>() {


    private val fragments = mutableListOf<Fragment>()

//    override fun getViewModelClass() = MainViewModel::class.java

    override fun getLayoutId() = R.layout.activity_main
    override fun showTitleBar() = false

    // 页面索引常量
    companion object {
        const val PAGE_WORKBENCH = 0
        const val PAGE_MAP = 1
    }


    override fun setupView() {
        super.setupView()

        if(UserConfig.user == null) {
            RouterManager.navigateToLogin()
            finish()
            return
        }
//        binding.viewModel = viewModel
        setupFragment()
        setupViewPager()
        setupBottomNavigation()

    }
    private fun setupFragment(){
        fragments.add(WorkbenchFragment())
        fragments.add(MapFragment())
    }

    private fun setupViewPager() {
        binding.viewPager.adapter =
            MainViewPagerAdapter(supportFragmentManager, lifecycle, fragments)
        binding.viewPager.offscreenPageLimit = fragments.size
        binding.viewPager.isUserInputEnabled = false
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                val itemId = when (position) {
                    PAGE_WORKBENCH -> R.id.fragment_workbench
                    PAGE_MAP -> R.id.fragment_map
                    else -> R.id.fragment_workbench
                }
                if (binding.bottomNav.selectedItemId != itemId) {
                    binding.bottomNav.selectedItemId = itemId
                }
            }})
    }

    private fun setupBottomNavigation() {
        binding.bottomNav.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.fragment_workbench -> {
                    binding.viewPager.setCurrentItem(PAGE_WORKBENCH, true)
                    return@setOnItemSelectedListener true
                }
                R.id.fragment_map -> {
                    binding.viewPager.setCurrentItem(PAGE_MAP, true)
                    return@setOnItemSelectedListener true
                }
                else -> return@setOnItemSelectedListener false
            }
        }
    }


    override fun handleBackPress() {

    }


}